package com.baichuan.zntx.handler;

import cn.hutool.json.JSONUtil;
import com.baichuan.zntx.domain.Message;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToByteEncoder;
import io.netty.util.CharsetUtil;
import lombok.AllArgsConstructor;

/**
 * @author mazhanliang
 */
@AllArgsConstructor
public class MessageEncodeHandler extends MessageToByteEncoder<Message> {
    // 数据分割符
   private String delimiter;
    @Override
    protected void encode(ChannelHandlerContext channelHandlerContext, Message message, ByteBuf byteBuf) throws Exception {
        byteBuf.writeBytes((JSONUtil.toJsonStr(message) + delimiter).getBytes(CharsetUtil.UTF_8));
    }
}
